const app = getApp();

Page({
  data: {
    time:'',
    phone:'',
    nowDate:'',
    addressId:'',
    addressList:{},
    goodsList:{},
    cart_ids:'',
    is_cart:1,
    goods_id:'',
    orderer_name:'',
    orderer_mobile:'',
    special_remark:'',
    card_remark:'',
    price:'',
    isName:'',
    nickname:'',
    bingPhone:''
  },
  onShareAppMessage() {
    return {
      title: '花礼鲜花玫瑰生日',
      desc: '花礼鲜花玫瑰生日',
      path: 'pages/todos/todos'
    };
  },
  onLoad(r){
    var d = new Date();
    var years = d.getFullYear();
    var month = d.getMonth()+1;
    var days = d.getDate()+1;  
    var that = this;
    my.httpRequest({
      url: app.geturl+'/feedback/get-tel',
      method: 'POST',
      dataType: 'json',
      success: function(res) {
        that.setData({
          phone: res.data.data.tel
        });
      }
    });
    that.setData({
      time:years+'-'+month+'-'+days,
      nowDate:years+'-'+month+'-'+days,
      cart_ids:r.cart_id,
      goods_id:r.goods_id,
      addressId:r.aid,
      is_cart:r.is_cart
    });
    that.orderList();
  },
  orderList(){
     var that = this;
     my.getStorage({
      key: 'token',
      success: function(res) {  
        my.httpRequest({
          url: app.geturl+'/settlement/list?token='+res.data.token,
          method: 'POST',
          data: {
            token:res.data.token,
            is_cart:that.data.is_cart,
            cart_ids:that.data.cart_id,
            goods_list:that.data.goods_id,
            member_address_id:that.data.addressId
          },
          dataType: 'json',
          success: function(res) {
            console.info(res.data.data.addressInfo);
            that.setData({
              addressList:res.data.data.addressInfo,
              goodsList:res.data.data.storeList[1].goods_list,
              price:res.data.data.totalAll
            });
          }
        }); 
      }
    });
  },
  onShow(){
    var that = this;
    my.getStorage({
      key: 'address',
      success: function(re) { 
        console.info(re);
        if(re.data!=null){
          that.setData({
            addressId:re.data.id
          });
        }
      }
    });
    that.orderList();
    that.checkPhone();
  },
  checkTime(){
    var that = this;
    var d = new Date();
    var years = d.getFullYear();
    var nowDate = that.data.nowDate;
    var endDate = (years+20)+'-12-30';
    my.datePicker({
      format: 'yyyy-MM-dd',
      currentDate: that.data.time,
      startDate: nowDate,
      endDate: endDate,
      success: (res) => {
        that.setData({
          time:res.date
        });
      },
    });
  },
  subOrder(){
    var that = this;
    var is_anonymous=0;
    if(that.data.isName=='cur'){
      is_anonymous = 1;
    }
    console.info(that.data);
    if(that.data.addressId>0){
      my.getStorage({
        key: 'token',
        success: function(res) {      
          my.httpRequest({
            url: app.geturl+'/order/create?token='+res.data.token,
            method: 'POST',
            data: {
              is_cart:that.data.is_cart,
              cart_ids:that.data.cart_ids,
              goods_list:that.data.goods_id,
              member_address_id:that.data.addressId,
              delivery_time:that.data.time,
              is_anonymous:is_anonymous,
              orderer_name:that.data.orderer_name,
              orderer_mobile:that.data.orderer_mobile,
              card_remark:that.data.card_remark,
              special_remark:that.data.special_remark,
            },
            dataType: 'json',
            success: function(re) {
              if(re.data.code==0){
                my.httpRequest({
                  url: app.geturl+'/pay/alipay-app',
                  method: 'GET',
                  data: {
                    token:res.data.token,
                    order_ids:re.data.data.orderIds,
                    order_type:'order'
                  },
                  dataType: 'json',
                  success: function(r) {
                    if(r.data.code==0){
                      my.tradePay({
                        orderStr: r.data.data.payStr, //完整的支付参数拼接成的字符串，从服务端获取
                        success: (rr) => {
                          my.redirectTo({
                            url: '../add-todo/order/'
                          })
                        },
                        fail: (rr) => {
                          my.redirectTo({
                            url: '../add-todo/order/'
                          })
                        }
                      });
                    }
                  }
                }); 
              }else{
                app.memberLogin();
              }
            }
          }); 
        }
      });
    }else{
      my.alert({content: '请添加收货地址'});
      return;
    }
    
  },
  checkPhone(){
    var that = this;
    my.getStorage({
      key: 'token',
      success: function(res) {
        if(typeof res.data!='undefined'){
          my.httpRequest({
            url: app.geturl+'/member/my-info',
            method: 'GET',
            data:{
              token:res.data.token
            },
            dataType: 'json',
            success: function(re) {
              if(re.data.code==0){
                that.setData({
                  bingPhone:re.data.data.Obj.mobile
                });
              }else{
                app.memberLogin();
              }
            }
          });
        }
      }
    });
  },
  cardRemark(e) {
    this.setData({
      card_remark:e.detail.value
    });
  },
  specialRemark(e) {
    this.setData({
      special_remark:e.detail.value
    });
  },
  ordererMobile(e) {
    this.setData({
      orderer_mobile:e.detail.value
    });
  },
  ordererName(e) {
    this.setData({
      orderer_name:e.detail.value
    });
  },
  onBlur(e) {
    this.setData({
      inputValue: e.detail.value,
    });
  },
  memberPhone(){
    my.navigateTo({ url: '../add-todo/member_phone' });
  },
  add() {
    app.todos = app.todos.concat([{
      text: this.data.inputValue,
      compeleted: false,
    }]);

    my.navigateBack();
  },
  isName(){
    var isName = this.data.isName;
    if(isName=='cur'){
      var name = '';
    }else{
      var name = 'cur';
    }
    this.setData({
      isName:name
    });
  },
  addressAdd(){
    my.navigateTo({ url: '../add-todo/address_add?type=1' });
  },
  makePhoneCall(e) {
    var that = this;
    my.makePhoneCall({ number: that.data.phone });
  },
  handleTap(){
    my.navigateTo({ url: '../todos/todos' });
  },
  addressList(){
    my.navigateTo({ url: '../add-todo/address_check?type=1' });
  }
});
